package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.DocumentDao;
import po.record.Document;

public class DocumentDaoImpl implements DocumentDao{
	private List<Document> documents;
	
	public DocumentDaoImpl() {
		documents = new LinkedList<>();
	}

	@Override
	public boolean insert(Document t) {
		if(documents.contains(t)) {
			return false;
		}
		return documents.add(t);
	}

	@Override
	public boolean update(Document t) {
		int index = documents.indexOf(t);
		if (index == -1) {
			return false;
		} else {
			documents.set(index, t);
			return true;
		}
	}

	@Override
	public boolean delete(int id) {
		Iterator<Document> doc = documents.iterator();
		while (doc.hasNext()) {
			Document catchd = doc.next();
			if (catchd.getDocId() == id) {
				catchd.setDocdeleteflag(true);
				return true;
			}
		}
		return false;
	}

	@Override
	public Document selectById(int id) {
		Iterator<Document> doc = documents.iterator();
		while (doc.hasNext()) {
			Document catchd = doc.next();
			if (id == catchd.getDocId()) {
				return catchd;
			}
		}
		return null;
	}

	@Override
	public List<Document> selectAll() {
		List<Document> copy = new LinkedList<>();
		for (Document tx : documents) {
			if (tx.isDocdeleteflag() == false) {
				copy.add(tx);
			}
		}
		return copy;
	}

}
